﻿using DL;
using DL.Repositories;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace BusinessService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SanPhamService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select SanPhamService.svc or SanPhamService.svc.cs at the Solution Explorer and start debugging.
    public class SanPhamService : ISanPhamService
    {
        public string CapNhatViTriSanPham(List<SanPham> sp)
        {
            var sanphamDataTier = new SanPhamRepository();
            var result = sanphamDataTier.CapNhatViTriSanPham(sp);
            return result;

        }
        public string CapNhatSanPhamNoiBat(List<SanPham> sp)
        {
            var sanphamDataTier = new SanPhamRepository();
            var result = sanphamDataTier.CapNhatSanPhamNoiBat(sp);
            return result;

        }
        public List<SanPham> DanhSachViTriSanPham()
        {
            try
            {
                var sanphamDataTier = new SanPhamRepository();
                var ListSanPham = new List<SanPham>();
                var dtbData = sanphamDataTier.DanhSachSanPhamSapXep();
                if (dtbData.Rows.Count > 0)
                {

                    int i = 0;
                    foreach (DataRow dr in dtbData.Rows)
                    {
                        var sp = new SanPham();
                        sp.Id = Convert.ToInt64(dr["S_Id"]);
                        sp.Ten = dr["S_Ten"].ToString();
                        sp.Anh = dr["S_Anh"].ToString();
                        sp.Gia = Convert.ToInt64(dr["S_Gia"]);
                        sp.SoLuong = Convert.ToInt64(dr["S_SoLuong"]);
                        sp.IdLoai = Convert.ToInt64(dr["LSP_Id"]);
                        sp.Cl = ((i % 2 == 0) ? "item_deal2" : "item_deal2 item_deal2_mid");
                        sp.TenLoai = dr["LSP_Ten"].ToString();
                        sp.CheckNoiBat = Convert.ToBoolean(dr["S_NoiBat"]);
                        sp.ViTri = Convert.ToInt64(dr["S_ViTri"]);
                        sp.RowNumber = Convert.ToInt64(dr["S_ROW"]);
                        ListSanPham.Add(sp);
                        i++;
                    }
                }
                return ListSanPham;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public DanhSachSanPham DanhSachSanPham(long idloai, int currPage, int recodperpage, int Pagesize, string func)
        {
            try
            {
                var sanphamDataTier = new SanPhamRepository();
                var ListSanPham = new List<SanPham>();
                var dsSanPham = new DanhSachSanPham();
                var dataTableBL = sanphamDataTier.DanhSachSanPham(idloai, currPage, recodperpage, Pagesize, func);
                DataTable dtbData = dataTableBL.Tables[0];
                if (dtbData.Rows.Count > 0)
                {

                    int i = 0;
                    foreach (DataRow dr in dtbData.Rows)
                    {
                        var sp = new SanPham();
                        sp.Id = Convert.ToInt64(dr["S_Id"]);
                        sp.Ten = dr["S_Ten"].ToString();
                        sp.Anh = dr["S_Anh"].ToString();
                        sp.Gia = Convert.ToInt64(dr["S_Gia"]);
                        sp.SoLuong = Convert.ToInt64(dr["S_SoLuong"]);
                        sp.IdLoai = Convert.ToInt64(dr["LSP_Id"]);
                        sp.Cl = ((i % 2 == 0) ? "item_deal2" : "item_deal2 item_deal2_mid");

                        sp.CheckNoiBat = Convert.ToBoolean(dr["S_NoiBat"]);
                        sp.TenLoai = dr["LSP_Ten"].ToString();
                        sp.TongSoBinhLuan = Convert.ToInt64(dr["S_TongSo"]);
                        //sp.ViTri = dr["S_ViTri"]!=null ?  Convert.ToInt64(dr["S_ViTri"]) : (long?)null;
                        ListSanPham.Add(sp);
                        i++;
                    }
                    dsSanPham.lstSanPham = ListSanPham;
                    dsSanPham.PhanTrangSP = dataTableBL.Tables[1].Rows[0]["PhanTrang"] + "";
                }
                return dsSanPham;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public List<SanPham> TimKiemSanPham(long idloai, string tukhoa)
        {
            try
            {
                var sanphamDataTier = new SanPhamRepository();
                var resultList = new List<SanPham>();
                var dataTable = sanphamDataTier.TimKiemSanPham(idloai, tukhoa);
                if (dataTable != null)
                {
                    int i = 0;
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        var sp = new SanPham();
                        sp.Id = Convert.ToInt64(dr["S_Id"]);
                        sp.Ten = dr["S_Ten"].ToString();
                        sp.Anh = dr["S_Anh"].ToString();
                        sp.Gia = Convert.ToInt64(dr["S_Gia"]);
                        sp.SoLuong = Convert.ToInt64(dr["S_SoLuong"]);
                        sp.IdLoai = Convert.ToInt64(dr["LSP_Id"]);
                        sp.Cl = ((i % 2 == 0) ? "item_deal2" : "item_deal2 item_deal2_mid");

                        sp.CheckNoiBat = Convert.ToBoolean(dr["S_NoiBat"]);
                        sp.TenLoai = dr["LSP_Ten"].ToString();
                        resultList.Add(sp);
                        i++;
                    }
                }
                return resultList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public List<LoaiSanPham> DanhSachLoaiSanPham()
        {
            try
            {
                var sanphamDataTier = new SanPhamRepository();
                var resultList = new List<LoaiSanPham>();
                var dataTable = sanphamDataTier.DanhSachLoaiSanPham();
                if (dataTable != null)
                {
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        var sp = new LoaiSanPham();
                        sp.Id = Convert.ToInt64(dr["LSP_Id"]);
                        sp.Ten = dr["LSP_Ten"].ToString();
                        sp.Ma = dr["LSP_Ma"].ToString();
                        sp.TrangThai = Convert.ToInt32(dr["LSP_TrangThai"]);
                        sp.SoLuongSP = Convert.ToInt64(dr["LSP_Sl"]);
                        resultList.Add(sp);
                    }
                }
                return resultList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// Chi tiết sản phẩm + bình luận
        /// </summary>
        /// <param name="IdSanPham"></param>
        /// <param name="currPage"></param>
        /// <param name="recodperpage"></param>
        /// <param name="Pagesize"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public SanPham ChiTietSanPham(long IdSanPham, int currPage, int recodperpage, int Pagesize, string func)
        {
            try
            {
                var sanphamDataTier = new SanPhamRepository();
                var resultList = new SanPham();
                var dataTable = sanphamDataTier.ChiTietSanPham(IdSanPham);
                if (dataTable != null && dataTable.Rows.Count == 1)
                {
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        resultList.Id = Convert.ToInt64(dr["S_Id"]);
                        resultList.Ten = dr["S_Ten"].ToString();
                        resultList.Anh = dr["S_Anh"].ToString();
                        resultList.Gia = Convert.ToInt64(dr["S_Gia"]);
                        resultList.SoLuong = Convert.ToInt64(dr["S_SoLuong"]);
                        resultList.IdLoai = Convert.ToInt64(dr["LSP_Id"]);
                        resultList.CheckNoiBat = Convert.ToBoolean(dr["S_NoiBat"]);
                    }

                }

                var listBinhLuan = new List<BinhLuan>();
                var danhsachBinhLuan = new DanhSachBinhLuan();
                var dataTableBL = sanphamDataTier.ChiTietBinhLuan(IdSanPham, currPage, recodperpage, Pagesize, func);
                DataTable dtbData = dataTableBL.Tables[0];
                if (dtbData.Rows.Count > 0)
                {
                    foreach (DataRow dr in dtbData.Rows)
                    {
                        var bl = new BinhLuan();
                        bl.Id = Convert.ToInt64(dr["B_Id"]);
                        bl.NoiDung = dr["B_NoiDung"].ToString();
                        bl.ThoiGian = Convert.ToDateTime(dr["B_ThoiGian"]);
                        bl.IdBinhLuanCha = Convert.ToInt64(dr["B_BinhLuanCha"]);
                        bl.Loai = "Hỏi";
                        listBinhLuan.Add(bl);

                        var dt_traloi = sanphamDataTier.ChiTietBinhLuanTraLoi(Convert.ToInt64(dr["B_Id"]));
                        if (dt_traloi.Rows.Count > 0)
                        {
                            foreach (DataRow dr1 in dt_traloi.Rows)
                            {
                                var tl = new BinhLuan();
                                tl.Id = Convert.ToInt64(dr1["B_Id"]);
                                tl.NoiDung = dr1["B_NoiDung"].ToString();
                                tl.ThoiGian = Convert.ToDateTime(dr1["B_ThoiGian"]);
                                tl.SanPhamId = Convert.ToInt64(dr1["S_Id"]);
                                tl.IdBinhLuanCha = Convert.ToInt64(dr1["B_BinhLuanCha"]);
                                tl.Loai = "AndyShop";
                                listBinhLuan.Add(tl);
                            }

                        }
                    }
                    danhsachBinhLuan.lstBinhLuan = listBinhLuan;
                    DataTable dtbPhanTrang = dataTableBL.Tables[1];
                    if (dtbPhanTrang.Rows.Count > 0)
                    {
                        //resultList.dsBinhLuan.PhanTrangBL = dtbPhanTrang.Rows[0]["PhanTrang"] + "";
                        danhsachBinhLuan.PhanTrangBL = dtbPhanTrang.Rows[0]["PhanTrang"] + "";

                    }
                    resultList.dsBinhLuan = danhsachBinhLuan;
                }

                return resultList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        /// <summary>
        /// Chi tiết nội dung sản phẩm
        /// </summary>
        /// <param name="currPage"></param>
        /// <param name="recodperpage"></param>
        /// <param name="Pagesize"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        /// 
        public SanPham ChiTietNoiDungSanPham(long IdSanPham)
        {
            try
            {
                var sanphamDataTier = new SanPhamRepository();
                var resultList = new SanPham();
                var dataTable = sanphamDataTier.ChiTietSanPham(IdSanPham);
                if (dataTable != null && dataTable.Rows.Count == 1)
                {
                    foreach (DataRow dr in dataTable.Rows)
                    {
                        resultList.Id = Convert.ToInt64(dr["S_Id"]);
                        resultList.Ten = dr["S_Ten"].ToString();
                        resultList.Anh = dr["S_Anh"].ToString();
                        resultList.Gia = Convert.ToInt64(dr["S_Gia"]);
                        resultList.SoLuong = Convert.ToInt64(dr["S_SoLuong"]);
                        resultList.IdLoai = Convert.ToInt64(dr["LSP_Id"]);
                        resultList.CheckNoiBat = Convert.ToBoolean(dr["S_NoiBat"]);
                    }

                }
                else {
                    return null;
                }

                return resultList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public DanhSachBinhLuan DanhSachBinhLuan(int currPage, int recodperpage, int Pagesize, string func)
        {
            try
            {
                var sanphamDataTier = new SanPhamRepository();
                var danhsachBinhLuan = new DanhSachBinhLuan();
                var lstBinhLuan = new List<BinhLuan>();
                var dataTableBL = sanphamDataTier.ChiTietBinhLuan(0, currPage, recodperpage, Pagesize, func);
                DataTable dtbData = dataTableBL.Tables[0];
                if (dtbData.Rows.Count > 0)
                {
                    foreach (DataRow dr in dtbData.Rows)
                    {
                        var bl = new BinhLuan();
                        bl.Id = Convert.ToInt64(dr["B_Id"]);
                        bl.NoiDung = dr["B_NoiDung"].ToString();
                        bl.ThoiGian = Convert.ToDateTime(dr["B_ThoiGian"]);
                        bl.SanPhamId = Convert.ToInt64(dr["S_Id"]);
                        bl.SanPhamTen = dr["S_Ten"].ToString();
                        bl.Loai = "Hỏi";
                        lstBinhLuan.Add(bl);

                        var dt_traloi = sanphamDataTier.ChiTietBinhLuanTraLoi(Convert.ToInt64(dr["B_Id"]));
                        if (dt_traloi.Rows.Count > 0)
                        {
                            foreach (DataRow dr1 in dt_traloi.Rows)
                            {
                                var tl = new BinhLuan();
                                tl.Id = Convert.ToInt64(dr1["B_Id"]);
                                tl.NoiDung = dr1["B_NoiDung"].ToString();
                                tl.ThoiGian = Convert.ToDateTime(dr1["B_ThoiGian"]);
                                tl.SanPhamId = Convert.ToInt64(dr1["S_Id"]);
                                tl.SanPhamTen = "";
                                tl.Loai = "AndyShop";
                                lstBinhLuan.Add(tl);
                            }

                        }


                    }
                    danhsachBinhLuan.lstBinhLuan = lstBinhLuan;
                    danhsachBinhLuan.PhanTrangBL = dataTableBL.Tables[1].Rows[0]["PhanTrang"] + "";
                }
                return danhsachBinhLuan;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public string CapNhatLoaiSanPham(LoaiSanPham lsp)
        {
            var sanphamDataTier = new SanPhamRepository();
            var result = sanphamDataTier.CapNhatLoaiSanPham(lsp);
            return result;
        }
        public string CapNhatSanPham(SanPham sp)
        {
            var sanphamDataTier = new SanPhamRepository();
            var result = sanphamDataTier.CapNhatSanPham(sp);
            return result;

        }

        public string CapNhatSanPhamChiTiet(SanPhamAnhCT anhct, SanPhamNoiDungCT noidungct)
        {
            var sanphamDataTier = new SanPhamRepository();
            var result = sanphamDataTier.CapNhatChiTietSanPham(anhct, noidungct);
            return result;
        }
    }
}
